//
//  CustomerDetailViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/9.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "CustomerDetailViewController.h"
#import "HttpCustomerAction.h"
#import "AddAssetViewController.h"
#import "AssetTableViewCell.h"
#import "AssetDetailViewController.h"

@interface CustomerDetailViewController ()
@property (nonatomic,strong) CustomerInfo *customerInfo;
@property (nonatomic,strong) NSArray *listArray;

@end

@implementation CustomerDetailViewController

-(id)initWithCustomerInfo:(CustomerInfo*)customerInfo{
    if (self == [super init]) {
        self.customerInfo = customerInfo;
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *rightbutton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
    rightbutton.titleLabel.font = [UIFont systemFontOfSize:16];
    [rightbutton setTitle:@"添加资产" forState:UIControlStateNormal];
    [rightbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(addAssetAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.tableView.separatorStyle = 0;
    self.tableView.tableHeaderView = [self getHeaderView];
    [self loadData];
}

-(void)addAssetAction:(id)sender{
    AddAssetViewController *addAssetViewController = [[AddAssetViewController alloc] initWithCustomerInfo:self.customerInfo block:^{
        [self loadData];
    }];
    [self.navigationController pushViewController:addAssetViewController animated:YES];
}

-(void)loadData{
    [HttpCustomerAction getCustomerInfo:self.customerInfo.userId block:^(id result, NSError *error) {
        if (!error) {
            self.listArray = result;
            [self.tableView reloadData];
            if (self.listArray.count == 0) {
                [self showNoResultWithFrame:CGRectMake(0, 360, SCREEN_WIDTH, SCREEN_HEIGHT-360)];
            }else{
                [self hiddenNoResultView];
            }
        }
    }];
}

-(UIView*)getHeaderView{
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 360)];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 320)];
    [bView addSubview:view];
    view.backgroundColor =DefaultBlueColor;
    UILabel *nameLabel = [UILabel new];
    nameLabel.text = self.customerInfo.realName;
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.font = [UIFont systemFontOfSize:30];
    [view addSubview:nameLabel];
    nameLabel.sd_layout.leftSpaceToView(view, 35).topSpaceToView(view, StatusBarHeight+44).rightSpaceToView(view, 35).heightIs(30);
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor whiteColor];
    [view addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(view, 35).rightSpaceToView(view, 35).topSpaceToView(nameLabel, 5).heightIs(1);
    
    UILabel *cardLabel = [UILabel new];
    cardLabel.text = [NSString stringWithFormat:@"身份证号:%@",self.customerInfo.idCard];
    cardLabel.textAlignment = NSTextAlignmentCenter;
    cardLabel.textColor = [UIColor whiteColor];
    cardLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:cardLabel];
    cardLabel.sd_layout.leftSpaceToView(view, 35).topSpaceToView(lineView, 0).rightSpaceToView(view, 35).heightIs(30);
    
    UIView *lineView1 = [UIView new];
    lineView1.backgroundColor = [UIColor whiteColor];
    [view addSubview:lineView1];
    lineView1.sd_layout.leftSpaceToView(view, 35).rightSpaceToView(view, 35).topSpaceToView(cardLabel, 0).heightIs(1);
    
    UILabel *totalLabel = [UILabel new];
    totalLabel.text = @"总资产(元)";
    totalLabel.textColor = [UIColor whiteColor];
    totalLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:totalLabel];
    totalLabel.sd_layout.leftSpaceToView(view, 35).topSpaceToView(lineView1, 10).rightSpaceToView(view, 35).heightIs(16);
    
    UILabel *totalMoneyLabel = [UILabel new];
    totalMoneyLabel.text = self.customerInfo.balance;
    totalMoneyLabel.textColor = [UIColor whiteColor];
    totalMoneyLabel.font = [UIFont systemFontOfSize:35];
    [view addSubview:totalMoneyLabel];
    totalMoneyLabel.sd_layout.leftSpaceToView(view, 35).topSpaceToView(totalLabel, 10).rightSpaceToView(view, 35).heightIs(36);
    
    UIView *backImageView = [UIView new];
    backImageView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2];
    [view addSubview:backImageView];
    backImageView.sd_layout.leftSpaceToView(view, 0).bottomSpaceToView(view, 0).rightSpaceToView(view, 0).heightIs(65);
    
    UILabel *totalEarnLabel = [UILabel new];
    totalEarnLabel.text = [NSString stringWithFormat:@"总收益\n%@",self.customerInfo.profit];
    totalEarnLabel.textColor = [UIColor whiteColor];
    totalEarnLabel.textAlignment = NSTextAlignmentCenter;
    totalEarnLabel.numberOfLines = 0;
    totalEarnLabel.font = [UIFont systemFontOfSize:14];
    [backImageView addSubview:totalEarnLabel];
    totalEarnLabel.sd_layout.leftSpaceToView(backImageView, 0).topSpaceToView(backImageView, 0).heightIs(65).widthIs(SCREEN_WIDTH/2);
    
    UILabel *integralLabel = [UILabel new];
    integralLabel.text = @"积分\n3000";
    integralLabel.textColor = [UIColor whiteColor];
    integralLabel.textAlignment = NSTextAlignmentCenter;
    integralLabel.numberOfLines = 0;
    integralLabel.font = [UIFont systemFontOfSize:14];
    [backImageView addSubview:integralLabel];
    integralLabel.sd_layout.leftSpaceToView(totalEarnLabel, 0).topSpaceToView(backImageView, 0).heightIs(65).widthIs(SCREEN_WIDTH/2);
    
    UILabel *tLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 320, SCREEN_WIDTH-20, 40)];
    tLabel.font = [UIFont systemFontOfSize:14];
    tLabel.text = @"投资记录";
    [bView addSubview:tLabel];
    
    return bView;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    AssetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[AssetTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    AssetInfo *assetInfo = self.listArray[indexPath.row];
    [cell setAssetCell:assetInfo];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AssetInfo *assetInfo = self.listArray[indexPath.row];

    AssetDetailViewController *assetDetailViewController = [[AssetDetailViewController alloc] initWithAssetInfo:assetInfo block:^{
        [self loadData];
    }];
    [self.navigationController pushViewController:assetDetailViewController animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
